package com.storeroompda.ui.scanfconfig;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.storeroompda.App;
import com.storeroompda.R;
import com.storeroompda.base.views.BaseActivity;
import com.storeroompda.base.views.BaseActivity_noMVP;
import com.storeroompda.entity.BillCodeControl;
import com.storeroompda.ui.config.ConfigModel;
import com.storeroompda.ui.config.ConfigModelImpl;
import com.storeroompda.ui.config.ConfigPresent;
import com.storeroompda.ui.config.ConfigView;
import com.storeroompda.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ScanfConfigActivity extends BaseActivity_noMVP {
    private String tag = "ScanfConfigActivity";
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.et_wll)
    EditText etWll;
    @BindView(R.id.et_wlh)
    EditText etWlh;
    @BindView(R.id.et_snl)
    EditText etSnl;
    @BindView(R.id.et_snh)
    EditText etSnh;
    @BindView(R.id.tvc1)
    TextView tvc1;
    @BindView(R.id.rl1)
    RelativeLayout rl1;
    @BindView(R.id.tvc2)
    TextView tvc2;
    @BindView(R.id.rl2)
    RelativeLayout rl2;
    @BindView(R.id.tvc3)
    TextView tvc3;
    @BindView(R.id.rl3)
    RelativeLayout rl3;
    @BindView(R.id.tvc4)
    TextView tvc4;
    @BindView(R.id.rl4)
    RelativeLayout rl4;
    @BindView(R.id.surebt)
    Button surebt;
    List<Integer> wls = new ArrayList<Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanfconfig);
        ButterKnife.bind(this);
        tvTitle.setText("扫描配置");
        initdata();
    }

    private void initdata()
    {
        etWll.setText(App.sSharedPreferences.getInt(Utils.wllkey,0)+"");
        etWlh.setText(App.sSharedPreferences.getInt(Utils.wlhkey,0)+"");
        etSnl.setText(App.sSharedPreferences.getInt(Utils.snlkey,0)+"");
        etSnh.setText(App.sSharedPreferences.getInt(Utils.snhkey,0)+"");
        tvc1.setText(App.sSharedPreferences.getString(Utils.eskey,getString(R.string.wlm)));
        tvc2.setText(App.sSharedPreferences.getString(Utils.sskey,getString(R.string.wlm)));
        tvc3.setText(App.sSharedPreferences.getString(Utils.rskey,getString(R.string.wlm)));
        tvc4.setText(App.sSharedPreferences.getString(Utils.cskey,getString(R.string.wlm)));
//        etWll.setText(App.sSharedPreferences.getInt(Utils.wllkey,1)+"");
//        etWlh.setText(App.sSharedPreferences.getInt(Utils.wlhkey,2)+"");
//        etSnl.setText(App.sSharedPreferences.getInt(Utils.snlkey,3)+"");
//        etSnh.setText(App.sSharedPreferences.getInt(Utils.snhkey,4)+"");
    }

    private boolean contain()
    {
        wls.clear();
        for (int i = Integer.parseInt(etWll.getText().toString()); i <=Integer.parseInt(etWlh.getText().toString()) ; i++) {
            wls.add(i);
        }
        for (int i = Integer.parseInt(etSnl.getText().toString()); i <=Integer.parseInt(etSnh.getText().toString()) ; i++) {
            for (int j = 0; j <wls.size() ; j++) {
                if(wls.get(j)==i)
                {
                    return true;
                }
            }
        }
        return  false;
    }

    @OnClick({R.id.iv_title_left, R.id.surebt,R.id.rl1, R.id.rl2, R.id.rl3, R.id.rl4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.surebt:
                if(Integer.parseInt(etWll.getText().toString())==0||Integer.parseInt(etSnl.getText().toString())==0)
                {
                    showMsg("条码长度不能为0");
                    return;
                }
                if(Integer.parseInt(etWll.getText().toString())>Integer.parseInt(etWlh.getText().toString()))
                {
                    showMsg("物流码长度不能从大到下");
                    return;
                }
                if(Integer.parseInt(etSnl.getText().toString())>Integer.parseInt(etSnh.getText().toString()))
                {
                    showMsg("SN码长度不能从大到下");
                    return;
                }
                if(contain())
                {
                    showMsg("两种码长的范围不能有交集");
                    return;
                }
                if(BillCodeControl.searchBillAll()!=null && BillCodeControl.searchBillAll().size()>0)
                {
                    showMsg("请先上传本地单据，再变更扫描类型");
                    return;
                }
                App.sEditor.putInt(Utils.wllkey,Integer.parseInt(etWll.getText().toString()));
                App.sEditor.putInt(Utils.wlhkey,Integer.parseInt(etWlh.getText().toString()));
                App.sEditor.putInt(Utils.snlkey,Integer.parseInt(etSnl.getText().toString()));
                App.sEditor.putInt(Utils.snhkey,Integer.parseInt(etSnh.getText().toString()));
                App.sEditor.putString(Utils.eskey,tvc1.getText().toString());
                App.sEditor.putString(Utils.sskey,tvc2.getText().toString());
                App.sEditor.putString(Utils.rskey,tvc3.getText().toString());
                App.sEditor.putString(Utils.cskey,tvc4.getText().toString());
                App.sEditor.commit();
                showMsg("扫描配置完成");
                finish();
                break;
            case R.id.rl1:
                showPopupWindow(view);
                break;
            case R.id.rl2:
                showPopupWindow(view);
                break;
            case R.id.rl3:
                showPopupWindow(view);
                break;
            case R.id.rl4:
                showPopupWindow(view);
                break;
        }
    }

    private void showPopupWindow(final View view) {
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_type, null);
        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        final TextView button = (TextView) contentView.findViewById(R.id.tv1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              switch (view.getId())
              {
                  case R.id.rl1:
                      tvc1.setText(getString(R.string.wlm));
                      break;
                  case R.id.rl2:
                      tvc2.setText(getString(R.string.wlm));
                      break;
                  case R.id.rl3:
                      tvc3.setText(getString(R.string.wlm));
                      break;
                  case R.id.rl4:
                      tvc4.setText(getString(R.string.wlm));
                      break;
                  default:
                      break;
              }
              popupWindow.dismiss();
            }
        });

        final TextView button1 = (TextView) contentView.findViewById(R.id.tv2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (view.getId())
                {
                    case R.id.rl1:
                        tvc1.setText(getString(R.string.snm));
                        break;
                    case R.id.rl2:
                        tvc2.setText(getString(R.string.snm));
                        break;
                    case R.id.rl3:
                        tvc3.setText(getString(R.string.snm));
                        break;
                    case R.id.rl4:
                        tvc4.setText(getString(R.string.snm));
                        break;
                    default:
                        break;
                }
                popupWindow.dismiss();
            }
        });

        final TextView button2 = (TextView) contentView.findViewById(R.id.tv3);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (view.getId())
                {
                    case R.id.rl1:
                        tvc1.setText(getString(R.string.wsm));
                        break;
                    case R.id.rl2:
                        tvc2.setText(getString(R.string.wsm));
                        break;
                    case R.id.rl3:
                        tvc3.setText(getString(R.string.wsm));
                        break;
                    case R.id.rl4:
                        tvc4.setText(getString(R.string.wsm));
                        break;
                    default:
                        break;
                }
                popupWindow.dismiss();
            }
        });
        popupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
        popupWindow.showAsDropDown(view);
        // 设置背景颜色变暗
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.5f;
        getWindow().setAttributes(lp);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 1f;
                getWindow().setAttributes(lp);
            }
        });
    }
}
